<template>
  <page-header-wrapper>
    <page-tab-list>
      <div class="table-page-search-box">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :md="8" :sm="24">
              <a-form-item label="商品分类">
                <div class="flex">
                  <a-input allow-clear placeholder="请输入商品分类" v-model="queryParam.name"></a-input>
                  <a-button type="primary" @click="getList()">查询</a-button>
                </div>
              </a-form-item>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </page-tab-list>

    <a-card :border="false">
      <div class="table-operator" style="display: flex; justify-content: space-between">
        <a-radio-group v-model="queryParam.status" @change="getList()">
          <a-radio-button :value="item.id" :key="item.id" v-for="item of statusList">
            {{ item.title }}
          </a-radio-button>
        </a-radio-group>

        <div style="display: flex">
          <a-button type="primary" style="margin-right: 10px" @click="handle('edit', 0)">+ 新增</a-button>
          <a-dropdown>
            <a-menu slot="overlay">
              <a-menu-item @click="handleAll('change', '1')">批量开启</a-menu-item>
              <a-menu-item @click="handleAll('change', '0')">批量关闭</a-menu-item>
              <a-menu-item @click="handleAll('change', '2')">批量删除</a-menu-item>
            </a-menu>
            <a-button> 批量操作 </a-button>
          </a-dropdown>
        </div>
      </div>

      <a-table
        style="width: 100%; overflow-x: auto"
        :rowSelection="{ selectedRowKeys: this.selectedRowKeys, onChange: this.onSelectChange }"
        size="default"
        :columns="columns"
        :data-source="list"
        :row-key="(record) => record.id"
        :pagination="{
          current: queryParam.pageNo,
          total: this.total,
          pageSize: this.pageSize,
          showSizeChanger: false,
          onChange: this.tablePageChange,
        }"
      >
        <span slot="is_show" slot-scope="text">
          <a-badge :color="getStatus(text)[1]" :text="getStatus(text)[0]" />
        </span>

        <span slot="action" slot-scope="text, record">
          <a-button type="link" size="small" @click="handle('edit', record)">编辑</a-button>
          <a-divider type="vertical" />
          <a-dropdown>
            <a-button type="link" size="small">更多<a-icon type="down" /></a-button>
            <a-menu slot="overlay">
              <a-menu-item key="0" v-if="!record.is_show">
                <a-button type="link" size="small" @click="handle('change', record, 1)">开启</a-button>
              </a-menu-item>
              <a-menu-item key="0" v-else>
                <a-button type="link" size="small" @click="handle('change', record, 0)">禁用</a-button>
              </a-menu-item>
              <a-menu-item key="0">
                <a-button type="link" size="small" @click="handle('change', record, 2)">删除</a-button>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </a-card>

    <pagePopForm ref="pop" @callback="getList()"></pagePopForm>
  </page-header-wrapper>
</template>

<script>
import { cateList, cateStatus } from '@/api/marketing/serviceHome'
import pagePopForm from './components/page-pop-form.vue'

const columns = [
  {
    title: 'ID',
    dataIndex: 'id',
    width: 80,
  },
  {
    title: '分类名称',
    dataIndex: 'name',
    ellipsis: true,
  },
  {
    title: '分类排序',
    dataIndex: 'sort',
    sorter: (a, b) => parseInt(a.sort) - parseInt(b.sort),
  },
  {
    title: '状态',
    dataIndex: 'is_show',
    scopedSlots: {
      customRender: 'is_show',
    },
  },
  {
    title: '创建时间',
    dataIndex: 'created_at',
    sorter: (a, b) => a.created_at - b.created_at,
  },
  {
    title: '操作',
    scopedSlots: {
      customRender: 'action',
    },
    width: 180,
  },
]

export default {
  components: {
    pagePopForm,
  },

  data() {
    return {
      total: 0,
      pageNo: 1,
      pageSize: 10,
      columns,
      queryParam: {
        name: '',
        status: '-1',
      },
      list: [],
      selectedRowKeys: [],
      statusList: [
        { id: '-1', title: '全部' },
        { id: '1', title: '已开启' },
        { id: '0', title: '已禁用' },
      ],
    }
  },

  mounted() {
    this.getList()
  },

  computed: {
    getStatus() {
      return (status) => {
        let text, color
        switch (String(status)) {
          case '0':
            text = '已禁用'
            color = 'rgba(0, 0, 0, 0.25)'
            break

          case '1':
            text = '已开启'
            color = '#65C817'
            break
        }

        return [text, color]
      }
    },
  },

  methods: {
    // 选中多行
    onSelectChange(selectedRowKeys) {
      this.selectedRowKeys = selectedRowKeys
    },

    // 批量操作
    handleAll(type, params) {
      let { selectedRowKeys } = this
      if (!selectedRowKeys.length) {
        this.$message.error('请至少选择一条数据~')
        return
      }

      switch (type) {
        case 'change':
          this.status({
            id: selectedRowKeys.join(','),
            is_show: params,
          })
          break
      }
    },

    // 获取数据列表
    async getList(pageNo = 1) {
      const hide = this.$message.loading('加载中', 0)
      let { queryParam, pageSize } = this
      const res = await cateList({
        ...queryParam,
        pageSize,
        pageNo,
      })
      if (res.code == 0) {
        this.pageNo = pageNo
        this.selectedRowKeys = []
        let { totalCount, data } = res.data
        this.total = totalCount
        this.list = data
      }
      hide()
    },

    // 分页请求
    tablePageChange(e) {
      this.getList(e)
    },

    // 点击单个
    handle(type, item, parmas) {
      switch (type) {
        case 'change':
          this.status({
            id: item.id,
            is_show: parmas,
          })
          break

        case 'edit':
          let _el = this.$refs.pop
          _el.visible = true
          if (item) {
            let { id, name, sort, is_show } = item
            _el.form = {
              id,
              name,
              sort,
              is_show,
            }
          }
          break
      }
    },

    status(data) {
      this.$confirm({
        title: '提示?',
        content: '是否执行此次操作？',
        onOk: async () => {
          const hide = this.$message.loading('请稍等...')
          const res = await cateStatus(data)
          hide()
          if (res.code) {
            this.$error({
              content: res.msg,
            })
            return
          }
          this.$message.success(res.msg)
          this.getList()
        },
        onCancel: () => {},
      })
    },
  },
}
</script>

<style lang="less" scoped>
.searchBox {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  padding: 8px 0 18px 0;
  /deep/ .ant-input-affix-wrapper {
    width: 220px;
    margin-right: 20px;
  }
  .select {
    display: flex;
    align-items: center;
    margin-right: 20px;
  }
}

.content {
  display: flex;
  align-items: center;
  height: 48px;
  max-width: 300px;
  overflow: hidden;

  .txt {
    margin-left: 16px;
    flex: 1;
    overflow: hidden;

    * {
      margin: 0;
      font-size: 14px;
      text-overflow: ellipsis;
      white-space: nowrap;
      overflow: hidden;
    }

    h3 {
      color: rgba(0, 0, 0, 0.85);
    }

    p {
      color: rgba(0, 0, 0, 0.45);
    }
  }
}

.flex {
  .inp {
    margin-right: 16px;
  }
}
</style>
